package cn.cqie.attendance.teacher.service.impl;

import cn.cqie.attendance.common.util.LocalJwt;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.teacher.dao.TeacherAttendanceDao;
import cn.cqie.attendance.teacher.pojo.TeacherAttendance;
import cn.cqie.attendance.teacher.pojo.TeacherAttendanceReq;
import cn.cqie.attendance.teacher.pojo.TeacherPunch;
import cn.cqie.attendance.teacher.service.TeacherAttdanceService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luwei
 * @date 2021/6/21 - 17:09
 */
@Service
public class TeacherAttendanceServiceImpl implements TeacherAttdanceService {
    @Autowired
    private TeacherAttendanceDao teacherAttendanceDao;

    @Autowired
    private HttpServletRequest request;

    @Override
    public PageBean<TeacherAttendance> queryAttendanceByTeacherId(TeacherAttendanceReq param) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
            param.setTeacherId(teacherId);
        if (param.getPageNum()==null || param.getPageSize()==null){
            param.setPageNum(1);
            param.setPageSize(10);
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(param.getPageNum());
        pageBean.setPageSize(param.getPageSize());
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<TeacherAttendance> teacherAttendances = teacherAttendanceDao.queryAttendanceByTeacherId(param);
        int total = teacherAttendances.size();

        if (null == teacherAttendances || total == 0) {
            return new PageBean<>(500,"查询列表为空");
        }
        return new PageBean<>(200, "Success",teacherAttendances);
    }

    /**
     * 教师查看课程下的学生的门禁打卡记录
     * @param
     * @return
     */
    @Override
    public PageBean<TeacherPunch> queryStudentPunchByTeacherId(String startTime, String endTime,
                                                           String stuNumber, String stuName, Integer pageNum,
                                                           Integer pageSize) {
        //当前登录教师id
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (pageNum==null || pageSize==null){
            pageNum=1;
            pageSize=10;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("stuNumber",stuNumber);
        map.put("stuName",stuName);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherPunch> teacherPunches = teacherAttendanceDao.queryStudentPunchByTeacherId(map);
        int total = teacherPunches.size();

        if (null == teacherPunches || total == 0) {
            return new PageBean<>(500,"查询列表为空");
        }
        return new PageBean<>(200, "Success",teacherPunches);
    }


}
